/**
 * ReportGear(2011)
 */
package net.abbp.report.view.cell.value;

import net.abbp.core.util.NumberUtils;
import net.abbp.core.util.StringUtils;
import net.abbp.report.model.ReportModel;
import net.abbp.report.model.io.IValueIO;

import org.dom4j.Element;

/**
 * Js数据源对象,当前主要处理薪酬对象
 * 
 * @version 1.0 2011-7-27
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class JsDataValue extends AbstractCellObject {
    // 普通
    public static final int TYPE_NORMAL = 0;
    // 相片
    public static final int TYPE_PHOTO = 1;
    // 公式
    public static final int TYPE_FORMULA = 2;
    // 数据源Id
    private String dataSourceId;
    // 数据源标签
    private String dataSourceCaption;
    // 字段名
    private String field;
    // 字段标签
    private String fieldCaption;
    // 扩展
    private boolean expand = false;
    // 类型
    private int type = TYPE_NORMAL;
    // 公式
    private String formula;

    @Override
    public String toString() {
        if (this.type == TYPE_FORMULA) {
            return ":" + this.formula;
        } else {
            return this.dataSourceCaption + "." + this.fieldCaption;
        }
    }

    /**
     * 设置数据源Id
     * 
     * @param dataSourceId 数据源Id
     */
    public void setDataSourceId(String dataSourceId) {
        this.dataSourceId = dataSourceId;
    }

    /**
     * 获取数据源Id
     * 
     * @return 数据源Id
     */
    public String getDataSourceId() {
        return dataSourceId;
    }

    /**
     * 设置字段
     * 
     * @param field 字段
     */
    public void setField(String field) {
        this.field = field;
    }

    /**
     * 获取字段
     * 
     * @return 字段
     */
    public String getField() {
        return field;
    }

    public void setDataSourceCaption(String dataSourceCaption) {
        this.dataSourceCaption = dataSourceCaption;
    }

    public String getDataSourceCaption() {
        return dataSourceCaption;
    }

    public void setFieldCaption(String fieldCaption) {
        this.fieldCaption = fieldCaption;
    }

    public String getFieldCaption() {
        return fieldCaption;
    }

    public void setExpand(boolean expand) {
        this.expand = expand;
    }

    public boolean isExpand() {
        return expand;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getType() {
        return type;
    }

    public void setFormula(String formula) {
        this.formula = formula;
    }

    public String getFormula() {
        return formula;
    }

    public static class IO implements IValueIO {
        @Override
        public Object read(Element value, ReportModel model) {
            JsDataValue jdv = new JsDataValue();

            jdv.setDataSourceId(value.elementText("DataSourceID"));
            jdv.setDataSourceCaption(value.elementText("DataSourceCaption"));
            jdv.setField(value.elementText("Field"));
            jdv.setFieldCaption(value.elementText("FieldCaption"));
            jdv.setType(NumberUtils.convertToInteger(value.elementText("Type")));
            jdv.setFormula(value.elementText("formula"));

            return jdv;
        }

        @Override
        public void write(Element element, Object value) {
            JsDataValue v = (JsDataValue) value;
            element.addElement("DataSourceID").addText(v.getDataSourceId());
            element.addElement("DataSourceCaption").addText(v.getDataSourceCaption());
            element.addElement("Field").addText(v.getField());
            element.addElement("FieldCaption").addText(v.getFieldCaption());
            element.addElement("Type").addText(String.valueOf(v.getType()));
            element.addElement("formula").addText(StringUtils.convertToString(v.getFormula(), ""));
        }

    }
}
